package com.tywj.navigation.tool

import android.graphics.Color
import android.location.Location
import com.amap.api.location.AMapLocation
import com.amap.api.maps.model.BitmapDescriptorFactory
import com.amap.api.maps.model.MyLocationStyle
import com.amap.api.services.core.PoiItem
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.location.entity.LocationEntity
import com.tywj.navigation.R

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/19
 *     desc   : 地图工具类
 *     version: 1.0.0
 * </pre>
 */
object MapTool {

    // 默认地图缩放级别
    const val ZOOM_LEVEL_DEFAULT = 16f

    // 默认城市
    var CITY_DEFAULT = "成都市"

    var CITY_CODE = "510100"

    // 默认纬度 //?lng=104.06889&lat=30.659075
    const val LATITUDE_DEFAULT = 104.06889

    // 默认经度
    const val LONGITUDE_DEFAULT = 30.659075

    // 当前用户位置
    var curLocation: LocationEntity? = null
        private set


    fun updateLocation(location: Location) {
        val entity = LocationEntity()
        entity.latitude = location.latitude
        entity.longitude = location.longitude

        val extras = location.extras
        entity.country = extras.getString("Country") ?: ""
        entity.countryCode = ""
        entity.province = extras.getString("Province") ?: ""
        entity.city = extras.getString("City") ?: ""
        entity.cityCode = extras.getString("CityCode") ?: ""
        entity.district = extras.getString("District") ?: ""
        entity.street = extras.getString("Street") ?: ""
        entity.streetNumber = extras.getString("Street") ?: ""
        entity.address = extras.getString("desc") ?: ""

        curLocation = entity
    }

    /**
     * 获取地址信息
     */
    fun getAddress(poi: PoiItem): String {
        val sb = StringBuilder()
        sb.append(poi.provinceName)
        if (poi.provinceName != poi.cityName) {
            sb.append(poi.cityName)
        }
        sb.append(poi.adName)
        sb.append(poi.snippet)

        return sb.toString()
    }


    // 定位时间间隔（毫秒）
    const val LOCATION_INTERVAL = 5000L

    fun getMyLocationStyle(): MyLocationStyle {
        val locationStyle = MyLocationStyle()
        // 设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        locationStyle.interval(LOCATION_INTERVAL)
        // 定位、但不会移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。
        locationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER)
        // 自定义定位蓝点图标 不设置为默认的高德的蓝色蓝点
        locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.navigation_icon_follow))
        // 设置圆形的填充颜色
        locationStyle.radiusFillColor(ResourcesUtil.getColor(R.color.common_transparent))
        // 自定义精度范围的圆形边框颜色
        locationStyle.strokeColor(ResourcesUtil.getColor(R.color.common_transparent))
        // 自定义精度范围的圆形边框宽度
        locationStyle.strokeWidth(1f)

        // 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
        locationStyle.showMyLocation(true)

        return locationStyle
    }

}